package com.filldream.fastboot.common.okutil.stock.cf.comment;

import cn.hutool.core.util.RandomUtil;
import com.filldream.fastboot.common.okutil.stock.BaseUtil;
import com.filldream.fastboot.common.okutil.stock.cf.comment.dto.CfComment;
import com.filldream.fastboot.common.okutil.stock.cf.comment.dto.CfCommentReply;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;

/**
 * 东方财富网-工具
 *
 * @version 2.0
 */
@Slf4j
public class CfCommentReadUtil {

    //已经显示且没有新回复的评论
    public static final List<CfComment> oldCommentList = new ArrayList<>();

    /**
     * 阅读
     * @param codes
     * @return
     */
    public static boolean reads(String codes){
        boolean isRefresh = false;
        String[] split = codes.split(",");
        for (String item : split) {
            boolean read = read(item);
            try {
                Thread.sleep(1000L * RandomUtil.randomLong(1L,3L));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            isRefresh = read || isRefresh;
        }
        return isRefresh;
    }

    /**
     * 阅读评论
     * @param code
     * @return
     */
    public static Boolean read(String code){
        List<CfComment> waitReadComments = getWaitReadComments(code);
        for (CfComment item : waitReadComments) {
            //输出内容
            String printContent = item.obtainPrintContent();
            if( printContent.length() > 90 ){
                continue;
            }
            System.out.println(BaseUtil.xrContent(printContent));
            List<String> post_pic_url = item.getPost_pic_url();
            for (String itemUrl : post_pic_url) {
                System.out.println(BaseUtil.xrReply("  -图片：" + BaseUtil.xrContent(itemUrl)));
            }
            List<CfCommentReply> realReplyList = item.obtainRealReplyList();
            for (CfCommentReply reply : realReplyList) {
                System.out.println(BaseUtil.xrReply(reply.getPrint()));
            }
        }
        return !waitReadComments.isEmpty();
    }

    /**
     * 获取最新的股评和回复
     * @param code
     * @return
     */
    public static List<CfComment> getWaitReadComments(String code){
        List<CfComment> waitReadComments = new ArrayList<>();
        //最新评论
        List<CfComment> comments = CfCommentUtil.getComments(code);
        for (CfComment comment : comments) {
            CfComment oldComment = oldCommentList.stream().filter(t -> t.getPost_id().equals(comment.getPost_id())).findFirst().orElse(null);
            //全新的
            if( oldComment == null ){
                waitReadComments.add( comment );
                oldCommentList.add(comment);
            }else{
                //查询一下没有输出的评论
                List<CfCommentReply> oldCommentRealReplyList = oldComment.getReply_list();
                List<CfCommentReply> realReplyList = comment.obtainRealReplyList();
                List<CfCommentReply> waitReadReplyList = new ArrayList<>();
                for (CfCommentReply cfCommentReply : realReplyList) {
                    Long replyId = cfCommentReply.getReply_id();
                    if( oldCommentRealReplyList.stream().noneMatch(t -> t.getReply_id().equals(replyId)) ){
                        waitReadReplyList.add( cfCommentReply );
                    }
                }
                oldCommentRealReplyList.addAll( waitReadReplyList );
                oldComment.setReply_list( oldCommentRealReplyList );
                if( !waitReadReplyList.isEmpty() ){
                    waitReadComments.add( oldComment );
                    oldCommentList.removeIf(t -> t.getPost_id().equals(oldComment.getPost_id()));
                    oldCommentList.add(oldComment);
                }
            }
        }
        return waitReadComments;
    }


}
